MineModel = {}
local self = MineModel
Model:Register(self)

function self:Init()
    self.diff_lev = MineConstant.DiffLev.I
    self.max_x = 0
    self.max_y = 0
    self.max_bomb_count = 0
    self.grids = {}
    self.bomb_count = 0
    self.game_status = MineConstant.GameStatus.Node
end

function self:SetDiff(idx)
    self.diff_lev = idx
    local diff_args = MineConstant.DiffArgs[self.diff_lev]
    self.max_x = diff_args.X
    self.max_y = diff_args.Y
    self.max_bomb_count = diff_args.BombCount
end

function self:MakeGrid(idx, x, y)
    return {
        idx = idx,
        x = x,
        y = y,
        bomb_count = 0,
        is_bomb = false,
        is_dig = false
    }
end

function self:AddGrid(grid)
    local arr_x = self.grids[grid.x]
    if arr_x == nil then
        arr_x = {}
        self.grids[grid.x] = arr_x
    end
    arr_x[grid.y] = grid
end

function self:GetGrid(x, y)
    local arr_x = self.grids[x]
    if arr_x then
        return arr_x[y]
    end
    return nil
end

function self:AddBomb()
    local x = math.random(self.max_x)
    local y = math.random(self.max_y)
    local grid = self:GetGrid(x, y)
    if grid then
        if not grid.is_bomb then
            grid.is_bomb = true
            self.bomb_count = self.bomb_count + 1
        end
    end
end

function self:ForeachGrid(func)
    for x, arr_x in pairs(self.grids) do
        for y, grid in pairs(arr_x) do
            func(grid)
        end
    end
end

function self:IsBombFull()
    return self.bomb_count >= self.max_bomb_count
end

function self:IsBomb(x, y)
    local grid = self:GetGrid(x, y)
    if grid and grid.is_bomb then
        return true
    end
    return false
end




